// pages/information/information.js
wx.cloud.init()
var db = wx.cloud.database({
  env: "zousonglin1992-n4jsc"
})
Page({

  /**
   * 页面的初始数据
   */
  data: {
    make: false,
    vipPrice: null,
    userName: null,
    userVipId: null,
    userVip: null,
    userCode: null
  },
  makeVip: function(e) {
    var that = this
    console.log(e)
    wx.showLoading({
      title: '加载中...',
    })
    wx.getStorage({
      key: 'phone',
      success: function(res) {
        if (e.detail.value.userName == "") {
          wx.hideLoading()
          setTimeout(function() {
            wx.showModal({
              title: '提示',
              content: '请输入你的姓名',
              confirmText: "好的",
              confirmColor: "#F5B223",
              showCancel: false
            })
          }, 0)
        } else if (e.detail.value.userId == "") {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '请输入你的身份证号码',
            confirmText: "好的",
            confirmColor: "#F5B223",
            showCancel: false
          })
        } else {
          wx.request({
            url: 'https://op.juhe.cn/idcard/query',
            data: {
              key: "a61feaa769d336d29437d93e30074f06",
              idcard: e.detail.value.userId,
              realname: e.detail.value.userName
            },
            success(res) {
              console.log(res)
              var msg = res.data.reason
              if (res.data.result != null && res.data.result.res === 1) {
                wx.cloud.callFunction({
                  name: "addUserId",
                  data: {
                    userId: e.detail.value.userId
                  }
                }).then(res => {
                  console.log(res)
                  if (res.result.data.length === 0) {
                    wx.cloud.callFunction({
                      name: "pay",
                      data: {
                        orderid: Math.floor(Math.random() * 1000000),
                        money: that.data.vipPrice * 100
                      },
                      success(res) {
                        console.log("提交成功", Math.floor(Math.random() * 1000000))
                        console.log("提交成功", res.result)
                        wx.hideLoading()
                        // that.pay(res.result)
                        wx.requestPayment({
                          timeStamp: res.result.timeStamp,
                          nonceStr: res.result.nonceStr,
                          package: res.result.package, //统一下单接口返回的 prepay_id 格式如：prepay_id=***
                          signType: 'MD5',
                          paySign: res.result.paySign, //签名
                          success(res) {
                            console.log("支付成功", res)
                            wx.getStorage({
                              key: 'phoneNumber',
                              success: function(res) {
                                console.log("phoneNumber", res.data)
                                db.collection("UserId").add({
                                  data: {
                                    客户姓名: e.detail.value.userName,
                                    客户身份证号码: e.detail.value.userId,
                                    客户手机号码: res.data,
                                    客户vip信息: "青松VIP会员",
                                    客户vip积分: 2000
                                  }
                                }).then(res => {
                                  console.log("成功", res)
                                  wx.switchTab({
                                    url: '/pages/user/user',
                                    success(res) {
                                      wx.showToast({
                                        title: '开通会员成功',
                                        duration: 2000
                                      })
                                    }
                                  })
                                })
                                // wx.clearStorage()
                              }
                            })
                          },
                          fail(res) {
                            console.log("支付失败", res)
                          },
                          complete(res) {
                            console.log("支付完成", res)
                          }
                        })
                      },
                      fail(err) {
                        console.log("提交失败", err)
                      }
                    })
                  } else {
                    wx.hideLoading()
                    setTimeout(function() {
                      wx.showModal({
                        title: '提示',
                        content: '该账户已开通VIP,积分使用完再开通吧😀',
                        confirmText: "好的",
                        confirmColor: "#F5B223",
                        showCancel: false
                      })
                    }, 0)
                  }
                })
              } else if (res.data.result != null && res.data.result.res === 2) {
                wx.hideLoading()
                setTimeout(function() {
                  wx.showModal({
                    title: '提示',
                    content: "请填写正确的身份证信息",
                    confirmText: "好的",
                    confirmColor: "#F5B223",
                    showCancel: false
                  })
                }, 0)
              } else if (res.data.error_code === 210304) {
                wx.hideLoading()
                setTimeout(function() {
                  wx.showModal({
                    title: '提示',
                    content: "姓名或身份证格式错误",
                    confirmText: "好的",
                    confirmColor: "#F5B223",
                    showCancel: false
                  })
                }, 0)
              }
            }
          })
        }
      },
      fail(err) {
        setTimeout(function() {
          wx.showModal({
            title: '提示',
            content: '请绑定您的手机号码',
            confirmText: "好的",
            confirmColor: "#F5B223",
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/loginPhone/loginPhone',
                })
              }
            }
          })
        }, 0)
      }
    })
  },
  getVipPrice() {
    var that = this
    db.collection("Vip-Price").get().then(res => {
      console.log("VIP价格", res)
      that.setData({
        vipPrice: res.data[0].vipPrice
      })
    })
  },
  getUserVipInfo() {
    var that = this
    wx.cloud.callFunction({
      name: "getUserVipInfo"
    }).then(res => {
      console.log("判断是否是vip用户", res)
      if (res.result.data.length == 0) {
        that.setData({
          hasVip: true
        })
      } else if (res.result.data[0].客户vip积分 === 0) {
        let userVipId = res.result.data[0].客户身份证号码.slice(0, 3) + "**********" + res.result.data[0].客户身份证号码.slice(-4, -1)
        that.setData({
          hasVip: false,
          userName: res.result.data[0].客户姓名,
          userVipId: userVipId,
          userVip: "普通会员",
          userCode: res.result.data[0].客户vip积分
        })
      } else {
        let userVipId = res.result.data[0].客户身份证号码.slice(0, 3) + "**********" + res.result.data[0].客户身份证号码.slice(-4, -1)
        that.setData({
          hasVip: false,
          userName: res.result.data[0].客户姓名,
          userVipId: userVipId,
          userVip: "青松VIP会员",
          userCode: res.result.data[0].客户vip积分
        })
      }
    })
  },
  again() {
    var that = this
    wx.showModal({
      title: '续费会员',
      content: '是否续费会员',
      confirmColor: "#F5B223",
      confirmText: "是的",
      success(res) {
        if (res.confirm) {
          wx.cloud.callFunction({
            name: "getUserVipInfo"
          }).then(res => {
            console.log("判断是否是vip用户", res)
            if (res.result.data[0].客户vip积分 === 0) {
              var userId = res.result.data[0].客户身份证号码
              wx.cloud.callFunction({
                name: "pay",
                data: {
                  orderid: Math.floor(Math.random() * 1000000),
                  money: that.data.vipPrice * 100
                },
                success(res) {
                  console.log("提交成功", Math.floor(Math.random() * 1000000))
                  console.log("提交成功", res.result)
                  wx.hideLoading()
                  // that.pay(res.result)
                  wx.requestPayment({
                    timeStamp: res.result.timeStamp,
                    nonceStr: res.result.nonceStr,
                    package: res.result.package, //统一下单接口返回的 prepay_id 格式如：prepay_id=***
                    signType: 'MD5',
                    paySign: res.result.paySign, //签名
                    success(res) {
                      console.log("支付成功", res)
                      wx.cloud.callFunction({
                        name: "xufei",
                        data: {
                          userId: userId
                        }
                      }).then(res => {
                        console.log("续费成功", res)
                        wx.switchTab({
                          url: '/pages/user/user',
                          success(res) {
                            wx.showToast({
                              title: '会员续费成功',
                              duration: 2000
                            })
                          }
                        })
                      })
                    },
                    fail(res) {
                      console.log("支付失败", res)
                    },
                    complete(res) {
                      console.log("支付完成", res)
                    }
                  })
                },
                fail(err) {
                  console.log("提交失败", err)
                }
              })
            } else {
              setTimeout(function() {
                wx.showModal({
                  title: '提示',
                  content: '亲,积分使用完再开通吧😀',
                  confirmText: "好的",
                  confirmColor: "#F5B223",
                  showCancel: false
                })
              }, 0)
            }
          })
        }
      }
    })
  },
  goVipZhuanXiang() {
    wx.navigateTo({
      url: '/pages/zhuanxiang/zhuanxiang',
    })
  },
  getVipZhuanxiang() {
    var that = this
    db.collection("VipZhuanXiang").get().then(res => {
      console.log("专享",res)
      that.setData({
        vipZhuanXiangData: res.data[0].produce
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUserVipInfo()
    this.getVipPrice()
    this.getVipZhuanxiang()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#F5B223'
    })
    this.getUserVipInfo()
    this.getVipPrice()
    this.getVipZhuanxiang()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})